package servlet.admin;

import dao.admin.ManagerEditDao;
import entity.Util;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import service.admin.ManagerEditService;
import service.admin.impl.ManagerEditServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

/**
 * ManagerEditServlet
 *
 * @author ChenYanMei
 * @version 2023/9/5
 */
@WebServlet("/admin/managerEdit")
public class ManagerEditServlet extends HttpServlet {
    ManagerEditService service = new ManagerEditServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("application/json; charset=utf8");
        // 接收参数
        String id = req.getParameter("id");
        String name = req.getParameter("name");
        String password = req.getParameter("password");
        String email = req.getParameter("email");
        // 准备参数
        String sql = "";
        Connection connection = null;
        PreparedStatement pstmt = null;
        ResultSet resultSet = null;
        int result = 0;
        // 返回参数
        int code = 1;
        String msg = "";
        PrintWriter out = resp.getWriter();
        JSONArray jsonArray = new JSONArray();
        JSONObject jsonObject  = new JSONObject();
        // 进行查询
        if(name==null || name.equals("")|| password==null || password.equals("") || email==null || email.equals("")) {
            msg = "参数不能为空";
            out.print(Util.jsonResponse(code, msg, null));
        }else {
            result = service.update(name, password, email, Integer.valueOf(id));
            // 返回数据
            if(result == 1 ){
                code = 0;
                msg = "修改成功";
            }else {
                msg = "修改失败";
            }
            out.print(Util.jsonResponse(code, msg, null));
        }

    }
}
